import time

import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from src.PO.base import Base
from src.PO.product_addition import ProductAddition
from src.PO.product_edit_page import ProductEidtPage
from src.PO.product_search_page import ProductSearchPage


@allure.feature("商品列表界面")
class ProductListPage(Base):
    __ADD_BUTTON=(By.XPATH," //*[text()='添加'] ")
    __LINK=(By.XPATH,"//*[text()='黑神话悟空']")
    __BELETE_BUTTON=(By.CSS_SELECTOR,".el-table__body>tbody>tr:nth-child(1)>td:nth-child(12)>div>button:nth-child(2)")
    __SEARCH_ID=(By.CSS_SELECTOR,"[placeholder='请输入商品ID']")
    __SEARCH_NUMBER=(By.CSS_SELECTOR,"[placeholder='请输入商品编号']")
    __SEARCH_NAME=(By.CSS_SELECTOR,"[placeholder='请输入商品名称']")
    __SEARCH=(By.CSS_SELECTOR,".el-icon-search")
    __EDIT_BUTTON = (By.CSS_SELECTOR, "tbody>tr:nth-child(1)>td:nth-child(12)>div>button:nth-child(1)")

    # def __init__(self,driver:WebDriver):
    #     self.driver=driver
    def click_to_add(self):
        with allure.step("点击添加商品按钮"):
            # self.driver.find_element(*self.__ADD_BUTTON).click()
            self.find_and_click(*self.__ADD_BUTTON)
        return ProductAddition(self.driver)

    def click_to_edit(self):
        with allure.step("点击编辑"):
            WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located((self.__EDIT_BUTTON)))
            self.find_and_click(*self.__EDIT_BUTTON)
        return ProductEidtPage(self.driver)

    def click_to_delete(self):
        with allure.step("点击删除按钮"):
            self.find_and_click(*self.__BELETE_BUTTON)

    def click_to_id_search(self):
        with allure.step("输入商品ID查找"):
            self.find_and_send(*self.__SEARCH_ID,text="1036016")
        with allure.step("点击查找"):
            self.find_and_click(*self.__SEARCH)
            #显式等待, 判断查找按钮 是否点击
            WebDriverWait(self.driver,10).until(expected_conditions.element_to_be_clickable((self.__SEARCH)))
        return ProductSearchPage(self.driver)
    def click_to_number_search(self):
        with allure.step("输入商品编号查找"):
            self.find_and_send(*self.__SEARCH_NUMBER,text="1036016")
        with allure.step("点击查找"):
            self.find_and_click(*self.__SEARCH)
            WebDriverWait(self.driver,10).until(expected_conditions.element_to_be_clickable((self.__SEARCH)))
        return ProductSearchPage(self.driver)
    def click_to_name_search(self):
        with allure.step("输入商品名称查找"):
            self.find_and_send(*self.__SEARCH_NAME, text="日式色织水洗棉床笠")
        with allure.step("点击查找"):
            self.find_and_click(*self.__SEARCH)
            WebDriverWait(self.driver,10).until(expected_conditions.element_to_be_clickable((self.__SEARCH)))
        return ProductSearchPage(self.driver)
    def click_to_accurate_search(self):
        with allure.step("精准查找"):
            self.find_and_send(*self.__SEARCH_ID, text="1036016")
            self.find_and_send(*self.__SEARCH_NUMBER, text="1036016")
            self.find_and_send(*self.__SEARCH_NAME, text="日式色织水洗棉床笠")
            self.find_and_click(*self.__SEARCH)
            # 显式等待, 判断查找按钮 是否点击
            WebDriverWait(self.driver,10).until(expected_conditions.element_to_be_clickable((self.__SEARCH)))
        return ProductSearchPage(self.driver)
    def click_to_edit_product_search(self):
        with allure.step("精准查找编辑商品"):
            self.find_and_send(*self.__SEARCH_ID, text="1036016")
            self.find_and_click(*self.__SEARCH)
            WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((self.__SEARCH)))
        return ProductSearchPage(self.driver)

    def get_product(self):
        with allure.step("获取添加商品信息"):
            # pr_list=self.driver.find_elements(*self.__LINK)
            pr_list=self.finds(*self.__LINK)
            product_list=[pr.text for pr in pr_list]
        return product_list
